package com.test.spring.master;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

public class DynamicDataSourceHolder {
	
	private static final Logger LOGGER = LoggerFactory.getLogger(DynamicDataSourceHolder.class);
	
	//写库对应的数据源key
	private static final String MASTER = "master";
	
	//读库对应的数据源key 
	private static final String SLAVE = "slave";
	
    //使用ThreadLocal记录当前线程的数据源key  
    private static final ThreadLocal<String> holder = new ThreadLocal<String>(); 
	
    /** 
     * 设置数据源key 
     * @param key 
     */  
    public static void putDataSourceKey(String key) {
    	if (LOGGER.isDebugEnabled()) {
    		LOGGER.debug("------切换数据源，key:"+key+"-------");
		}
        holder.set(key);  
    }
    
    /** 
     * 获取数据源key 
     * @return 
     */  
    public static String getDataSourceKey() {  
        return holder.get();  
    }
    
    /** 
     * 标记写库 
     */  
    public static void markMaster(){  
        putDataSourceKey(MASTER);  
    }  
      
    /** 
     * 标记读库 
     */  
    public static void markSlave(){  
        putDataSourceKey(SLAVE);  
    }
}
